/**
 * @apiDefine ErrorResponse
 * @apiError (请求错误) {String} err_message 错误信息描述
 */

/**
 * @apiVersion 1.0.0
 * @apiGroup 商品
 * @api {POST} api/product/delete 删除商品
 * @apiName api/product/delete
 * @apiExample {js} axios 例子:
 *  axios.post('http://39.105.38.34:8080/api/product/delete', {
 *    id: 1,
 *  });
 * @apiHeader {String} authorization 用户登录或注册成功时返回的 token
 * @apiParam  {Number} id 商品 ID
 * @apiSuccess (请求成功) {Null} data 删除成功后不返回数据
 * @apiuse  ErrorResponse
 */

 const {del ,query} = require('../../helper/db');
 const { ok, door} = require('../../helper/util');

module.exports = async ctx => {
    var token = ctx.headers.authorization;
    var params = ctx.request.body;
    await door(token);
    var arr1 = await query('select * from productImg where productID = ?',[params.id])
    var arr2 = await query('select * from productDetail where productID = ?',[params.id])
    var arr3 = await query('select * from productDetail where productID = ?',[params.id])

    await del('product', params.id);
    arr1.forEach(item => {
      del('productImg',item.id)
    });
    arr2.forEach(item => {
      del('productDetail',item.id)
    });
    arr3.forEach(item => {
      del('productDetail',item.id)
    });
  
    ctx.body = ok(null);
  }